package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gexin.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.mapper.BankerBankerLabelMapper;
import com.ytjj.qmyx.admin.mapper.BankerLabelMapper;
import com.ytjj.qmyx.admin.model.*;
import com.ytjj.qmyx.admin.model.request.UpdateLabelRequest;
import com.ytjj.qmyx.admin.service.BankerLabelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class BankerLabelServiceImpl implements BankerLabelService {

    @Resource
    private BankerLabelMapper bankerLabelMapper;
    @Resource
    private BankerBankerLabelMapper bankerBankerLabelMapper;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Override
    public CommonResult select(String bankerLabelName, Integer pageNum, Integer pageSize) {
        BankerLabelExample bankerLabelExample = new BankerLabelExample();
        if (!StringUtils.isEmpty(bankerLabelName)){
            bankerLabelExample.createCriteria().andBankerLabelNameLike(bankerLabelName);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<BankerLabel> productLabels = bankerLabelMapper.selectByExample(bankerLabelExample);
        productLabels.stream().forEach(item ->{
            BankerBankerLabelExample bankerBankerLabelExample = new BankerBankerLabelExample();
            bankerBankerLabelExample.createCriteria().andBankerLabelIdEqualTo(item.getBankerLabelId());
            long count = bankerBankerLabelMapper.countByExample(bankerBankerLabelExample);
            item.setUseNum(Math.toIntExact(count));
            Admin admin = adminMapper.selectByPrimaryKey(item.getAdminId());
            item.setAdminName(admin.getCompellation());
        });
        return CommonResult.success(CommonPage.restPage(productLabels));
    }

    @Override
    public CommonPage newSelect(String bankerLabelName, Integer pageNum, Integer pageSize) {
        Map map = new HashMap<>();
        map.put("bankerLabelName",bankerLabelName);
        map.put("pageNum",String.valueOf(pageNum));
        map.put("pageSize",String.valueOf(pageSize));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/bankeLabelList", map, supplyApiConfig.getHeadsMap());
        CommonPage commonPage = JSONObject.parseObject(resultJson, CommonPage.class);
        List<BankerLabel> list = JSONArray.parseArray(commonPage.getList().toString(), BankerLabel.class);
        list.stream().forEach(item -> {
            Admin admin = adminMapper.selectByPrimaryKey(item.getAdminId());
            item.setAdminName(admin.getCompellation());
        });
        commonPage.setList(list);
        return commonPage;
    }

    @Override
    public CommonResult addAndUpdate(String bankerLabelName, Integer bankerLabelId, Integer adminId, String colour) {
        BankerLabelExample productLabelExample = new BankerLabelExample();
        if (null == bankerLabelId){
            productLabelExample.createCriteria().andBankerLabelNameEqualTo(bankerLabelName);
            long count = bankerLabelMapper.countByExample(productLabelExample);
            if (count > 0){
                throw new ApiException("该标签名已存在");
            }
            BankerLabel bankerLabel = new BankerLabel();
            bankerLabel.setBankerLabelName(bankerLabelName);
            bankerLabel.setCreateTime(new Date());
            bankerLabel.setAdminId(adminId);
            bankerLabel.setColour(colour);
            int i = bankerLabelMapper.insertSelective(bankerLabel);
            if (i > 0){
                return CommonResult.success("添加标签成功");
            }else {
                return CommonResult.failed("添加标签失败");
            }

        }else {
            productLabelExample.createCriteria().andBankerLabelNameEqualTo(bankerLabelName).andBankerLabelIdNotEqualTo(bankerLabelId);
            long count = bankerLabelMapper.countByExample(productLabelExample);
            if (count > 0){
                throw new ApiException("该标签名已存在");
            }
            BankerLabel bankerLabel = new BankerLabel();
            bankerLabel.setBankerLabelName(bankerLabelName);
            bankerLabel.setAdminId(adminId);
            bankerLabel.setColour(colour);
            bankerLabel.setBankerLabelId(bankerLabelId);
            int i = bankerLabelMapper.updateByPrimaryKeySelective(bankerLabel);
            if (i > 0){
                return CommonResult.success("修改标签成功");
            }else {
                return CommonResult.failed("修改标签失败");
            }
        }
    }

    @Override
    public String newAddAndUpdate(String bankerLabelName, Integer bankerLabelId, Integer adminId, String colour) {
        Map map = new HashMap();
        map.put("bankerLabelName",bankerLabelName);
        if (null == bankerLabelId) {
            map.put("bankerLabelId", "");
        }else {
            map.put("bankerLabelId", String.valueOf(bankerLabelId));
        }
        map.put("colour",colour);
        map.put("adminId", String.valueOf(adminId));
        map.put("shopId",supplyApiConfig.getShopId());
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/addAndUpdate", map, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public CommonResult delete(Integer bankerLabelId) {
        BankerBankerLabelExample bankerBankerLabelExample = new BankerBankerLabelExample();
        bankerBankerLabelExample.createCriteria().andBankerLabelIdEqualTo(bankerLabelId);
        long count = bankerBankerLabelMapper.countByExample(bankerBankerLabelExample);
        if (count > 0){
            throw new ApiException("该标签正在使用,不能删除");
        }
        int i = bankerLabelMapper.deleteByPrimaryKey(bankerLabelId);
        if (i > 0){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    @Override
    public String newDelete(Integer bankerLabelId) {
        Map map = new HashMap();
        map.put("bankerLabelId", String.valueOf(bankerLabelId));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/delete", map, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String selectAll() {
        Map map = new HashMap<>();
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/bankeLabelAllList", map, supplyApiConfig.getHeadsMap());
        return resultJson;

    }

    @Override
    public CommonResult addBankerLabel(List<Integer> bankerIds, List<Integer> bankerLabelIds,Integer adminId) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        bankerIds.stream().forEach(bankerId ->{
            BankerBankerLabelExample bankerBankerLabelExample = new BankerBankerLabelExample();
            bankerBankerLabelExample.createCriteria().andBankerIdEqualTo(bankerId);
            bankerBankerLabelMapper.deleteByExample(bankerBankerLabelExample);
            bankerLabelIds.stream().forEach(bankerLabelId ->{
                BankerBankerLabel bankerBankerLabel = new BankerBankerLabel();
                bankerBankerLabel.setAdminId(adminId);
                bankerBankerLabel.setBankerId(bankerId);
                bankerBankerLabel.setBankerLabelId(bankerLabelId);
                bankerBankerLabel.setCreateTime(new Date());
                bankerBankerLabelMapper.insertSelective(bankerBankerLabel);
                count.getAndSet(count.get() + 1);
            });
        });
        return CommonResult.success(count.get());
    }

    @Override
    public String newAddBankerLabel(UpdateLabelRequest updateLabelRequest) {
        String json = JSON.toJSONString(updateLabelRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/addBankerLabel", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }
}
